package org.xlin.biz.dao.menu;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.xlin.biz.dao.AbstBaseDao;
import org.xlin.biz.util.UtilHandler;

import com.google.common.collect.ImmutableMap;

/**
 * 
 * 菜单数据处理类
 * @author xl
 *
 */
@Repository
public class MenuDao extends AbstBaseDao<Map<String,?>>{
	
	/**
	 * 增加菜单
	 */
	private final static String AddRecord = " insert into t_mg_menu(code,name,price) "
			+ " values(:code,:name,:price) ";
	
	/**
	 * 修改菜单(不修正价格-若价格修正要考虑留底问题＝＝＝>现为若价格修正逻辑删除菜单后重建)
	 */
	private final static String ModfiyRecord = " update t_mg_menu t "
			+ " set t.code=:code, "
			+ " t.name=:name "
			+ " where t.id=:id ";
	
	/**
	 * 删除菜单(逻辑删除)
	 */
	private final static String DeleteRecord = " update t_mg_menu t set t.valid='0' where t.id=:id ";
	
	/**
	 * 查询就餐记录的点餐单(菜单不判定是否逻辑删除-便于追溯历史菜单)
	 */
	private final static String QueryMenusByRepastId = " select t.*,m.id as mid,m.num,m.state from t_mg_menu t,t_mid_repast_menu m"
			+ " where m.menuid=t.id and m.repastid=? order by m.created";
	
	/**
	 * 查询所有的菜单（加载菜单）
	 */
	private final static String QueryAllMenus4LoadSels = " select t.* from t_mg_menu t where t.valid='1' ";
	
	/**
	 * 分页查询菜单列表
	 * @param pageable
	 * @param condition
	 * @return
	 */
	public Page<Map<String, ?>> queryRecord4Page(Pageable pageable, Map<String, Object> condition){
		//查询列表Sql
        StringBuilder querySql = new StringBuilder();
        querySql.append(" select t.* from t_mg_menu t where t.valid='1' ")
        	.append(" {whereSql} ");
        //查询记录数Sql
        StringBuilder countSql = new StringBuilder();
        countSql.append(" select count(t.id) from t_mg_menu t where t.valid='1' ")
	    	.append(" {whereSql} ");
        //查询条件Sql
        StringBuilder whereSql = this.getWhereSqlByParseCondition(condition);
        return this.queryPage4Mysql(querySql, countSql, whereSql, pageable, condition, new RowMapper<Map<String, ?>>() {
            @Override
            public Map<String, ?> mapRow(ResultSet rs, int rowNum) throws SQLException {
                return ImmutableMap.of("ID", rs.getObject("ID"),
                		"CODE", rs.getObject("CODE"),
                		"NAME", rs.getObject("NAME"),
                		"PRICE", rs.getObject("PRICE"),
                		"CREATED", rs.getObject("CREATED"));
            }
        });
	}
	
	/**
     * 条件解析
     * @param condition
     * @return
     */
    public StringBuilder getWhereSqlByParseCondition(Map<String, Object> condition) {
        StringBuilder whereSql = new StringBuilder();
        //菜单名称或者菜单编码
        if (condition.containsKey("searchName")
        		&& !"".equals(condition.get("searchName"))) {
        	whereSql.append(" and (t.name like :searchName escape '/' ")
        		.append(" or t.code like :searchName escape '/' ) ");
        	condition.put("searchName", "%" + UtilHandler.handleSqlParam((String) condition.get("searchName")) + "%");
        }
        return whereSql;
    }
    
    /**
	 * 增加菜单
	 * @param condition
	 * @return
	 */
	public void addRecord(Map<String,Object> condition){
		this.namesUpdateForMap(AddRecord, condition);
	}
	
	/**
	 * 更新菜单
	 * @param condition
	 * @return
	 */
	public int modfiyRecord(Map<String,Object> condition){
		return this.namesUpdateForMap(ModfiyRecord, condition);
	}
	
	/**
	 * 删除菜单
	 * @param condition
	 * @return
	 */
	public void deleteRecord(Map<String,Object> condition){
		this.namesUpdateForMap(DeleteRecord, condition);
	}
	
	/**
	 * 查询就餐记录的点餐单
	 * @param repastId
	 * @return
	 */
	public List<Map<String,Object>> queryMenusByRepastId(int repastId){
		return this.getJdbcTemplate().queryForList(QueryMenusByRepastId, repastId);
	}
	
	/**
	 * 查询所有菜单
	 * @return
	 */
	public List<Map<String, Object>> queryAllMenus4LoadSels(){
		return this.getNamedParameterJdbcTemplate().query(QueryAllMenus4LoadSels, new RowMapper<Map<String, Object>>() {
			@Override
			public Map<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException {
				return ImmutableMap.of("id", rs.getObject("id"),
                		"code", rs.getObject("code"),
                		"name", rs.getObject("name"),
                		"price", rs.getObject("price"),
                		"created", rs.getObject("created"));
			}
		});
	}

}
